<template>
  <div class="min-h-screen bg-gray-50">
    <!-- 头部导航 -->
    <header class="bg-white shadow-sm border-b">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between items-center h-16">
          <!-- Logo -->
          <div class="flex items-center">
            <div class="h-8 w-8 bg-gradient-to-r from-blue-600 to-indigo-600 rounded-lg flex items-center justify-center">
              <el-icon class="text-white" :size="18">
                <ChatRound />
              </el-icon>
            </div>
            <h1 class="ml-3 text-xl font-bold text-gray-900">AI角色扮演</h1>
          </div>

          <!-- 用户菜单 -->
          <div class="flex items-center space-x-4">
            <!-- 通知 -->
            <el-badge :value="3" class="cursor-pointer">
              <el-icon :size="20" class="text-gray-500 hover:text-gray-700">
                <Bell />
              </el-icon>
            </el-badge>

            <!-- 用户头像菜单 -->
            <el-dropdown trigger="click">
              <div class="flex items-center space-x-2 cursor-pointer">
                <el-avatar :size="36" :src="userAvatar">
                  <el-icon><User /></el-icon>
                </el-avatar>
                <span class="text-sm font-medium text-gray-700">{{ userDisplayName }}</span>
                <el-icon class="text-gray-400" :size="14">
                  <ArrowDown />
                </el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="goToProfile">
                    <el-icon><User /></el-icon>
                    个人资料
                  </el-dropdown-item>
                  <el-dropdown-item @click="goToVoiceSettings">
                    <el-icon><Microphone /></el-icon>
                    语音设置
                  </el-dropdown-item>
                  <el-dropdown-item @click="goToSettings">
                    <el-icon><Setting /></el-icon>
                    系统设置
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </header>

    <!-- 主要内容 -->
    <main class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
      <div class="px-4 py-6 sm:px-0">
        <!-- 欢迎卡片 -->
        <div class="bg-gradient-to-r from-blue-600 to-indigo-600 rounded-xl shadow-lg p-6 mb-8">
          <div class="flex items-center justify-between">
            <div class="text-white">
              <h2 class="text-2xl font-bold mb-2">欢迎回来，{{ userDisplayName }}！</h2>
              <p class="text-blue-100">开始您的AI角色扮演之旅</p>
            </div>
            <div class="hidden md:block">
              <el-icon class="text-white opacity-20" :size="80">
                <ChatRound />
              </el-icon>
            </div>
          </div>
        </div>

        <!-- 功能卡片网格 -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
          <!-- 开始聊天 -->
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 hover:shadow-xl transition-all cursor-pointer" @click="goToChat">
            <div class="flex items-center justify-between mb-4">
              <div class="h-12 w-12 bg-green-100 rounded-lg flex items-center justify-center">
                <el-icon class="text-green-600" :size="24">
                  <ChatLineRound />
                </el-icon>
              </div>
              <el-button type="primary" size="small" @click.stop="goToChat">开始</el-button>
            </div>
            <h3 class="text-lg font-semibold text-gray-900 mb-2">AI聊天</h3>
            <p class="text-gray-600 text-sm">与AI角色进行实时语音和文字交流</p>
          </div>

          <!-- 角色管理 -->
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 hover:shadow-xl transition-all cursor-pointer" @click="goToCharacterManagement">
            <div class="flex items-center justify-between mb-4">
              <div class="h-12 w-12 bg-purple-100 rounded-lg flex items-center justify-center">
                <el-icon class="text-purple-600" :size="24">
                  <Avatar />
                </el-icon>
              </div>
              <el-button type="primary" size="small" plain @click.stop="goToCharacterManagement">管理</el-button>
            </div>
            <h3 class="text-lg font-semibold text-gray-900 mb-2">角色管理</h3>
            <p class="text-gray-600 text-sm">创建和自定义您的AI角色</p>
          </div>

          <!-- 语音设置 -->
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 hover:shadow-xl transition-all cursor-pointer" @click="goToVoiceSettings">
            <div class="flex items-center justify-between mb-4">
              <div class="h-12 w-12 bg-blue-100 rounded-lg flex items-center justify-center">
                <el-icon class="text-blue-600" :size="24">
                  <Microphone />
                </el-icon>
              </div>
              <el-button type="primary" size="small" plain @click.stop="goToVoiceSettings">设置</el-button>
            </div>
            <h3 class="text-lg font-semibold text-gray-900 mb-2">语音设置</h3>
            <p class="text-gray-600 text-sm">调整语音识别和合成参数</p>
          </div>
        </div>

        <!-- 统计信息 -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 text-center">
            <div class="text-3xl font-bold text-blue-600 mb-2">12</div>
            <div class="text-sm text-gray-600">今日对话</div>
          </div>
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 text-center">
            <div class="text-3xl font-bold text-green-600 mb-2">5</div>
            <div class="text-sm text-gray-600">创建角色</div>
          </div>
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 text-center">
            <div class="text-3xl font-bold text-purple-600 mb-2">2h</div>
            <div class="text-sm text-gray-600">语音时长</div>
          </div>
          <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6 text-center">
            <div class="text-3xl font-bold text-orange-600 mb-2">98%</div>
            <div class="text-sm text-gray-600">满意度</div>
          </div>
        </div>

        <!-- 最近活动 -->
        <div class="bg-white rounded-xl shadow-lg border border-gray-100 p-6">
          <h3 class="text-lg font-semibold text-gray-900 mb-4">最近活动</h3>
          <div class="space-y-4">
            <div class="flex items-center space-x-4">
              <div class="h-10 w-10 bg-blue-100 rounded-full flex items-center justify-center">
                <el-icon class="text-blue-600" :size="16">
                  <ChatLineRound />
                </el-icon>
              </div>
              <div class="flex-1">
                <p class="text-sm font-medium text-gray-900">与小助手进行了对话</p>
                <p class="text-xs text-gray-500">2分钟前</p>
              </div>
            </div>
            <div class="flex items-center space-x-4">
              <div class="h-10 w-10 bg-green-100 rounded-full flex items-center justify-center">
                <el-icon class="text-green-600" :size="16">
                  <Avatar />
                </el-icon>
              </div>
              <div class="flex-1">
                <p class="text-sm font-medium text-gray-900">创建了新角色"智能导师"</p>
                <p class="text-xs text-gray-500">1小时前</p>
              </div>
            </div>
            <div class="flex items-center space-x-4">
              <div class="h-10 w-10 bg-purple-100 rounded-full flex items-center justify-center">
                <el-icon class="text-purple-600" :size="16">
                  <Setting />
                </el-icon>
              </div>
              <div class="flex-1">
                <p class="text-sm font-medium text-gray-900">更新了语音设置</p>
                <p class="text-xs text-gray-500">3小时前</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  ChatRound,
  ChatLineRound,
  Avatar,
  Microphone,
  User,
  Bell,
  ArrowDown,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

// 路由和状态
const router = useRouter()
const authStore = useAuthStore()

// 计算属性
const userDisplayName = computed(() => authStore.userDisplayName)
const userAvatar = computed(() => authStore.userAvatar)

/**
 * 跳转到AI聊天页面
 */
const goToChat = () => {
  router.push('/chat')
}

/**
 * 跳转到角色管理页面
 */
const goToCharacterManagement = () => {
  ElMessage.info('角色管理功能开发中...')
}

/**
 * 跳转到个人资料页面
 */
const goToProfile = () => {
  router.push('/profile')
}

/**
 * 跳转到语音设置页面
 */
const goToVoiceSettings = () => {
  router.push('/voice-settings')
}

/**
 * 跳转到设置页面
 */
const goToSettings = () => {
  ElMessage.info('系统设置功能开发中...')
}

/**
 * 处理退出登录
 */
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '确认退出',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )

    await authStore.logout()
    router.push('/login')
  } catch (error) {
    // 用户取消操作
  }
}
</script>

<style scoped>
/* 自定义样式 */
.cursor-pointer {
  cursor: pointer;
}

/* 卡片悬停效果 */
.hover\:shadow-xl:hover {
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .max-w-7xl {
    padding: 0 16px;
  }
  
  .grid-cols-1 {
    grid-template-columns: 1fr;
  }
}

/* 动画效果 */
.transition-all {
  transition: all 0.3s ease;
}

/* 渐变背景 */
.bg-gradient-to-r {
  background: linear-gradient(to right, var(--tw-gradient-stops));
}
</style>
